package AAA.service.impl;

import AAA.dao.AccountDao;
import AAA.dao.impl.AccountDaoImpl;
import AAA.exceptions.MoneyNotEnoughException;
import AAA.exceptions.TransferException;
import AAA.pojo.Account;
import AAA.service.AccountService;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao = new AccountDaoImpl();

    public void transfer(String fromActno, String toActno, double money) throws MoneyNotEnoughException, TransferException {
        //1.判断转出账户的余额是否充足select
        //2.如果转出的账户余额不足，提示用户
        //3.如果转出账户余额充足，更新转出余额update
        //4.更新转入账户余额update
        Account fromAct = accountDao.selectByActno(fromActno);
        if (fromAct.getBalance() < money) {
            throw new MoneyNotEnoughException("余额不足");
        }
        Account toAct = accountDao.selectByActno(toActno);
        fromAct.setBalance(fromAct.getBalance() - money);
        toAct.setBalance(toAct.getBalance() + money);
        int count = accountDao.updateByActno(fromAct);
        count += accountDao.updateByActno(toAct);
        if (count != 2) {
            throw new TransferException("转账异常，未知原因");
        }


    }
}
